<template src="./template.html"></template>

<style src="./style.css" scoped></style>
<script>
    import uncheckedIconBg from "assets/images/unchecked-circle.png";
    import checkedIconBg from "assets/images/checked-circle.png";
    import api from "api/api";
    import modal from "utils/modal";
    import jsbridge from "utils/jsbridge";
    import navigator from "utils/navigator";
    import notification from "utils/notification";
    import constant from "constants/constant";

    export default{
        data(){
            return{
                checkedIconBg: checkedIconBg,
                uncheckedIconBg: uncheckedIconBg,
                addrTitle: "新建收货地址",
                addr_id: null,
                from: null, //判断确认订单页面来源字段
                userName: '',
                phone: '',
                areaName: '', //省市区名称
                prov: null, //省id
                city: null, //市id
                area: null, //区id
                detailAddress: '',
                cellName: '',
                isDefault: false, //是否是默认收货地址
                errorTip: "",
                cityShow: false //是否显示省市区
            }
        },
        created(){
            let self = this;
            let config = weex.config;
            let addr = config.address;

            if(addr) {
                self.addrTitle = "编辑收货地址";
                self.initAddress(addr);
            }

            if(config.from) {
                self.from = config.from;
            }
        },
        methods: {
            setDefault(){
                let self = this;
                self.isDefault = !self.isDefault;
            },
            saveAddress(){
                let self = this;
                if(self.checkForm()) {
                    let param = {
                        model: 'weex',
                        name: self.userName,
                        phone: self.phone,
                        provid: self.prov,
                        cityid: self.city,
                        areaid: self.area,
                        neighbourhood: self.cellName,
                        info: self.detailAddress
                    };

                    if(self.addr_id) {
                        param.id = self.addr_id;
                    }

                    if(self.isDefault) {
                        param.default = "1";
                    }

                    api.sendHttp("saveAddressInfoApi",param).then(res =>{
                        if(self.from && self.from == "fromConfirm") { //是否来自确认订单页面
                            //确认订单收货地址变更事件
                            if(self.addr_id) { //编辑通知
                                 notification.send(constant.EVENT_EDIT_ADDRESS_FROM_CONFIRM,res.data);
                            } else { //新建通知
                                 notification.send(constant.EVENT_ADD_ADDRESS_FROM_CONFIRM,res.data);
                            }
                        } else {
                            notification.send(constant.EVENT_EDIT_ADDRESS,res.data);
                        }

                        navigator.pop();
                    },
                    res =>{
                        modal.toast(res.msg || "保存收货地址失败");
                    });
                }

            },
            getCity(e){
                this.cityShow = true;
                //隐藏软键盘
                jsbridge.hideKeyBoard();
            },
            getCityArea(e){
                let self = this;
                self.prov = e.provinceId;
                self.city = e.cityId;
                self.area = e.districtId;
                self.areaName = e.address;
            },
            checkForm(){
                let self = this;
                if(!self.userName) {
                    self.showErrorTip("收货人姓名不能为空");
                    return false;
                }

                if(!self.phone) {
                    self.showErrorTip("收货人电话号码不能为空");
                    return false;
                }

                if(self.phone && !/^1\d{10}$/.test(self.phone)) {
                    self.showErrorTip("收货人电话号码格式不正确");
                    return false;
                }

                if(!self.areaName){
                    self.showErrorTip("收货人所在区域不能为空");
                    return false;
                }

                if(self.cellName && self.cellName.length > 20){
                    self.showErrorTip("收货人小区名称限20字");
                    return false;
                }

                if(!self.detailAddress){
                    self.showErrorTip("收货人详细地址不能为空");
                    return false;
                }

                if(self.detailAddress && self.detailAddress.length > 60){
                    self.showErrorTip("收货人详细地址限60字");
                    return false;
                }

                return true;
            },
            initAddress(addr){
                let self = this;
                self.addr_id = addr.addr_id;
                self.userName = addr.addr_name;
                self.phone = addr.addr_phone;
                self.areaName = addr.addr_prov + ' ' + addr.addr_city + ' ' + addr.addr_area;
                self.prov = addr.addr_provid;
                self.city = addr.addr_cityid;
                self.area = addr.addr_areaid;
                self.detailAddress = addr.addr_info;
                self.cellName = addr.addr_neighbourhood;

                if(addr.addr_default == "1") self.isDefault = true;
            },
            showErrorTip(tip){
                let self = this;
                self.errorTip = tip;

                if(self.errorTip) {
                    setTimeout(function(){
                        self.errorTip = "";
                    },1500);
                }
            }
        }
    }
</script>
